<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>Not true!?</title>
</head>
<body>

<h2 class="title">15. <span class="pykeyword">not</span> true!?</h2>
<p>Suppose we want to have Reeborg keep walking until he finds a beeper, and
then turn himself off. An example of a starting position is below:</p>
<img alt="stop at beeper" src="../../images/intro/stop_at_beeper.png">
<p>One way to do this- in <i>pseudocode</i>- would be as follows:<br>
<br>
If on beeper,<br>
... stop;<br>
otherwise,<br>
... keep moving.<br>
<br>
Using what we have seen so far, we could translate this as:</p>
<pre>
<span class="keyword">if</span> on_beeper():
    turn_off()
<span class="keyword">else</span>:
    move()           
</pre>
This is only part of the solution as we have not told Reeborg to keep on
repeating. Let's ignore this for now. Instead, consider the equivalent
<i>pseudocode</i> formulation:<br>
<br>
If not on beeper,<br>
...keep moving;<br>
otherwise,<br>
...stop.<br>
<br>
While we have reversed the order of "keep moving" and "stop", you should
agree that the two ways are equivalent. The key point is the use of the
negation <span class="pykeyword">not</span> after the <span class="pykeyword">if</span> in the second case. Python also allows us to
negate some tests as follows:
<pre>
<span class="keyword">if not</span> on_beeper():
    move()
<span class="keyword">else</span>:
    turn_off()          
</pre>
<p>To transform this into a complete program, using what we have seen so far,
we could need to <span class="pykeyword">def</span>ine an instruction, and
then have it repeated often enough to ensure that Reeborg can complete its
task.</p>
<h3 class="try">Your turn</h3>
<p>Use the new Python keyword <span class="pykeyword">not</span> and rewrite
your jumping hurdle program.</p>
<hr width="50%">
<h3 class="suggested">Weeding and seeding time</h3>
<p>It is spring time. Reeborg's father has seeded the garden for the fall
harvest. In some places, two seeds have sprouted whereas in others none have.
A typical situation is shown below (file: harvest4.wld).</p>
<img alt="weeding" src="../../images/intro/harvest4.png">
<p>Help Reeborg weed the garden, so that there are no places where two
carrots (beepers) are, and reseed so that there is no places with no carrots.
The final situation should look as follows:</p>
<img alt="weeding" src="../../images/intro/harvest1.png">
<p>Here's a <i>suggestion</i> for part of the code:</p>
<pre>
<span class="comment"># introducing vocabulary related to the problem</span>
next_to_a_carrot = on_beeper
plant_carrot = put_beeper
pick_carrot = pick_beeper

<span class="keyword">def</span> one_carrot_only():
    <span class="keyword">if not</span> next_to_a_carrot():
        plant_carrot()      <span class=
"comment"># replace missing seed</span>
    <span class="keyword">else</span>:
        pick_carrot()
        <span class="keyword">if not</span> next_to_a_carrot(): <span class="comment"># oops!</span>
            plant_carrot()  <span class="comment"># we had removed the only one</span>
</pre>
<p>Note that, in reality, it is <b>not</b> a good idea to remove seedlings
and replant them right away!</p>
<center><a href="14-elif.htm"><img alt="previous" src=
"../../images/previous.png"> If, else, if, else, ....</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"16-while.htm">For a while <img alt="next" src=
"../../images/next.png"></a>
</center>
</body>
</html>
